# coding=utf-8

'''
Копирует шаблон стандартного компонента
'''
import shutil
from os import path, listdir

#
pathprefix = '.'
webroot = None

while(not path.exists(pathprefix + '/bitrix/components/bitrix')):
	pathprefix += '/..'
else:
	webroot = path.abspath(pathprefix)
	pathprefix = path.abspath(pathprefix + '/bitrix/components/bitrix')

#
componentname = raw_input('Imya komponenta vida `component.name`: ')

templates = listdir(pathprefix + '/' + componentname + '/templates')
for t, tpl in enumerate(templates):
	print '[%d] ' % t + tpl
	
#
copy_index = int( raw_input('Vyberi shablon (%d-%d): ' % (0, t)) )
templatename = templates[copy_index]

newname = raw_input('Vvedi imya, libo ostanetsya [%s]: ' % templatename)
newname = newname.strip() or templatename

source = pathprefix + '/' + componentname + '/templates/' + templatename
destination = webroot + '/bitrix/templates/.default/components/bitrix/' + componentname + '/' + newname

#
if path.exists(destination):
	confirm = raw_input('Shablon uje byl skopirovan. Steret i skopirovat snova? (Y/N): ')
	if confirm.upper() != 'Y':
		exit()
	else:
		shutil.rmtree(destination)

shutil.copytree(source, destination)